﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Snake.Core;

namespace Snake.Cache
{
   /// <summary>
   /// 浏览器缓存
   /// </summary>
   public class ContextCache
    {
       /// <summary>
       /// 获取一个实体
       /// </summary>
       /// <param name="objectKey"></param>
       /// <returns></returns>
       public static Object Get(String objectKey)
       {
           if (objectKey == null)
           {
               throw new Exception("this context key cache is empty");
           }
           return ContextMap[objectKey];
       }
       /// <summary>
       /// 加入一个实体
       /// </summary>
       /// <param name="objectKey"></param>
       /// <param name="objectValue"></param>
       public static void Put(String objectKey, Object objectValue)
       {
           if (string.IsNullOrEmpty(objectKey))
           {
               throw new Exception("this context key cache is empty");
           }
           ContextMap[objectKey] = objectValue;
       }


       /// <summary>
       /// 删除一个实体
       /// </summary>
       /// <param name="objectKey"></param>
       public static void Remove(String objectKey)
       {
           if (!string.IsNullOrEmpty(objectKey))
           {
               ContextMap.Remove(objectKey);
           }
       }

       /// <summary>
       /// 删除实体
       /// </summary>
       /// <param name="typeFullName"></param>
       /// <param name="id"></param>
       public static void Remove(String typeFullName, int id)
       {
           var objectKey = CacheKey.getObject(typeFullName, id);
           Remove(objectKey);
       }


       /// <summary>
       /// 清楚所有实体
       /// </summary>
       public static void Clear()
       {
           ContextMap.Clear();
       }

       /// <summary>
       /// 上下文缓存
       /// </summary>
       private static IDictionary ContextMap
       {
           get { return Session.getContextCache(); }
       }
    }
}
